package com.example.lot_lock;

import android.util.Log;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Udp_communication {

//-------------------发送注册申请----------识别码'1'-----------------
    public void registerSendUdp(String user_name, String user_pass, String user_phone){
        String data = "1" + user_name+ "+" +user_pass+ "+" +user_phone;//注册信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] buf = data.getBytes();
                    DatagramSocket sendSocket = new DatagramSocket();
                    InetAddress serveradd = InetAddress.getByName("39.103.138.93");
                    DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, serveradd, 75);
                    sendSocket.send(datagramPacket);
                    sendSocket.close();
                } catch (Exception e) {
                    Log.v("错误", e.toString());
                }
            }
        }).start();
    }
//-----------------发送登录申请-----------识别码'2'---------------------
public void loginSendUdp(String user_name, String user_pass, String user_phone){
    String data = "2" + user_name+ "+" +user_pass;//注册信息
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                byte[] buf = data.getBytes();
                DatagramSocket sendSocket = new DatagramSocket();
                InetAddress serveradd = InetAddress.getByName("39.103.138.93");
                DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, serveradd, 75);
                sendSocket.send(datagramPacket);
                sendSocket.close();
            } catch (Exception e) {
                Log.v("错误", e.toString());
            }
        }
    }).start();
}

//---------------------------------------------------
 /*   public void SendUdp(){
        String date = user_name.getText().toString() + "+" + user_pass.getText().toString() + "+" + user_phone.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] buf = date.getBytes();
                    DatagramSocket sendSocket = new DatagramSocket();
                    // Log.v("错误", "1");

                    InetAddress serveradd = InetAddress.getByName("39.103.138.93");
                    //Log.v("错误", "2");

                    DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, serveradd, 75);
                    //Log.v("错误", "3");

                    sendSocket.send(datagramPacket);
                    // Log.v("错误", "4");

                    sendSocket.close();
                } catch (Exception e) {
                    Log.v("错误", e.toString());
                    //Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT);
                }
            }
        }).start();
    }
*/

}
